#include "view_weather.h"
#include "app_trace.h"
#include "battery_ctrl.h"
#include "blecom.h"
#include "datetime.h"
#include "display_ctrl.h"
#include "display_interface.h"
#include "font_mngr.h"
#include "heart_rate_interface.h"
#include "lfs_file_manager_auto.h"
#include "math.h"
#include "platform_assert.h"
#include "rtc.h"
#include "sport_his.h"
#include "weather_ctrl.h"
#include "common/common.h"
#include "font/font.h"
#include "view_weather.inc"
#include "view.h"
#define LOG(fmt, ...) app_trace_log(fmt "\r\n", ##__VA_ARGS__)
void view_weather_display(int view)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 500;
        weather_record_t weather = {0};
        weather_ctrl_get(0, &weather); //获取天气
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.u_temp[0] = UIdatetime.date.month;
        disp_ctrl.com_data.u_temp[1] = UIdatetime.date.day;
        disp_ctrl.com_data.u_temp[2] = weather.humidity;
        disp_ctrl.com_data.i_temp[3] = weather_get_temperature_type();
        disp_ctrl.com_data.i_temp[4] = weather.type;
        if (disp_ctrl.com_data.i_temp[3] != WEATHER_TEMPERATURE_TYPE_C)
        {
            weather.temperature_min = round(32 + weather.temperature_min * 1.8);
            weather.temperature_max = round(32 + weather.temperature_max * 1.8);
        }
        disp_ctrl.com_data.i_temp[5] = weather.temperature_min;
        disp_ctrl.com_data.i_temp[6] = weather.temperature_max;
        disp_ctrl.com_data.i_temp[7] = weather.umbrella_need;
    }
    int month = 0;
    int day = 0;
    int humidity = 0;
    weather_temperature_type_t temperature_type = WEATHER_TEMPERATURE_TYPE_F;
    weather_type_t weather_type = WEATHER_TYPE_UNKNOW;
    int temperature_min = 0;
    int temperature_max = 0;
    bool umbrella_need = false;
#if 1
    month = disp_ctrl.com_data.u_temp[0];
    day = disp_ctrl.com_data.u_temp[1];
    humidity = disp_ctrl.com_data.u_temp[2];
    temperature_type = (weather_temperature_type_t)disp_ctrl.com_data.i_temp[3];
    weather_type = (weather_type_t)disp_ctrl.com_data.i_temp[4];
    temperature_min = disp_ctrl.com_data.i_temp[5];
    temperature_max = disp_ctrl.com_data.i_temp[6];
    umbrella_need = disp_ctrl.com_data.i_temp[7];
#else
#warning debug
    month = 11;
    day = 23;
    humidity = 123;
    temperature_type = WEATHER_TEMPERATURE_TYPE_C;
    // weather_type = WEATHER_TYPE_RAINY;
    // weather_type = WEATHER_TYPE_SNOW;//雪
    // weather_type = WEATHER_TYPE_HAZE;//雾霾
    // weather_type = WEATHER_TYPE_THUNDER;//雷电
    weather_type = WEATHER_TYPE_PARTLY_CLOUDY; //局部多云
    temperature_min = -43;
    temperature_max = -12;
    umbrella_need = false;
#endif
#define WEATHER_DATE_INTERVAL (8)
    draw_on_chip_image(14, 4, IMAGE_USER_WEATHER_DATE);
    draw_on_chip_image(0, 25, IMAGE_USER_NUM8X11_TAB[month / 10 % 10]);
    draw_on_chip_image(WEATHER_DATE_INTERVAL * 1, 25, IMAGE_USER_NUM8X11_TAB[month % 10]);
    draw_on_chip_image(WEATHER_DATE_INTERVAL * 2, 24, IMAGE_USER_NUM8X11_LINE);
    draw_on_chip_image(WEATHER_DATE_INTERVAL * 2 + 6, 25, IMAGE_USER_NUM8X11_TAB[day / 10 % 10]);
    draw_on_chip_image(WEATHER_DATE_INTERVAL * 3 + 6, 25, IMAGE_USER_NUM8X11_TAB[day % 10]);

#define HUMIDITY_NUM_INTERVAL_X (9)

    uint8_t temp_start_x = 46;
    draw_on_chip_gray_image(56, 3, IMAGE_USER_WEATHER_HUMIDITY, 0x461f);
    if (humidity >= 100)
    {
        temp_start_x = 42 + HUMIDITY_NUM_INTERVAL_X;
        humidity = 0;
        draw_on_chip_image(42, 25, IMAGE_USER_NUM8X11_TAB[1]);
    }

    draw_on_chip_image(temp_start_x, 25, IMAGE_USER_NUM8X11_TAB[humidity / 10 % 10]);
    draw_on_chip_image(temp_start_x + HUMIDITY_NUM_INTERVAL_X * 1, 25, IMAGE_USER_NUM8X11_TAB[humidity % 10]);
    draw_on_chip_image(temp_start_x + HUMIDITY_NUM_INTERVAL_X * 2, 25, IMAGE_USER_WEATHER_HUMIDITY_PERCENT);

    draw_horizontal_line(0, 79, 41, 0x3186);
    draw_vertical_line(40, 0, 41, 0x3186);

//温度
#define WEATHER_TEMP_NUM_INTERVAL (13)
    if (temperature_min <= -10)
    {
        temp_start_x = 13;
    }
    else if (temperature_min < 0)
    {
        temp_start_x = 26;
    }
    else if (temperature_min >= 10)
    {
        temp_start_x = 20;
    }
    else
    {
        temp_start_x = 30;
    }

    if (temperature_min < 0)
    {
        temperature_min = -temperature_min;
        draw_on_chip_image(temp_start_x, 124, IMAGE_USER_NUM_NEGATIVE);
        temp_start_x += WEATHER_TEMP_NUM_INTERVAL;
    }
    temperature_min = temperature_min % 100; //只显示低两位
    if (temperature_min >= 10)
    {
        draw_on_chip_image(temp_start_x, 118, IMAGE_USER_NUM11X15_TAB[temperature_min / 10 % 10]);
        temp_start_x += WEATHER_TEMP_NUM_INTERVAL;
    }
    draw_on_chip_image(temp_start_x, 118, IMAGE_USER_NUM11X15_TAB[temperature_min % 10]);
    draw_on_chip_image(temp_start_x + WEATHER_TEMP_NUM_INTERVAL, 124, IMAGE_USER_WEATHER_TE_LINE);

    if (temperature_max <= -10)
    {
        temp_start_x = 13;
    }
    else if (temperature_max < 0)
    {
        temp_start_x = 26;
    }
    else if (temperature_max >= 10)
    {
        temp_start_x = 20;
    }
    else
    {
        temp_start_x = 30;
    }

    if (temperature_max < 0)
    {
        temperature_max = -temperature_max;
        draw_on_chip_image(temp_start_x, 149, IMAGE_USER_NUM_NEGATIVE);
        temp_start_x += WEATHER_TEMP_NUM_INTERVAL;
    }
    temperature_max = temperature_max % 100; //只显示低两位
    if (temperature_max >= 10)
    {
        draw_on_chip_image(temp_start_x, 143, IMAGE_USER_NUM11X15_TAB[temperature_max / 10 % 10]);
        temp_start_x += WEATHER_TEMP_NUM_INTERVAL;
    }
    draw_on_chip_image(temp_start_x, 143, IMAGE_USER_NUM11X15_TAB[temperature_max % 10]);
    temp_start_x += WEATHER_TEMP_NUM_INTERVAL;

    if (temperature_type == 1)
    {
        draw_on_chip_image(temp_start_x, 141, IMAGE_USER_WEATHER_TE_C);
    }
    else
    {
        draw_on_chip_image(temp_start_x, 141, IMAGE_USER_WEATHER_TE_F);
    }

    //带伞
    if (umbrella_need)
    {
        draw_on_chip_gray_image(65, 96, IMAGE_USER_WEATHER_UMBRELLA, 0x43f);
    }

    //天气类型
    // static uint8_t disp_cnt = 0;
    // uint8_t temp8;
    switch (weather_type)
    {
    case WEATHER_TYPE_SUNSHINE:
        draw_on_chip_gif_array(21, 56, zip_gif_WEATHER_SUNSHINE, sizeof(zip_gif_WEATHER_SUNSHINE) / sizeof(zip_gif_WEATHER_SUNSHINE[0]));
        draw_on_chip_image_ext(21 + 8, 56 + 7, IMAGE_USER_WEATHER_SUN, draw_type_filter_color);
        break;
    case WEATHER_TYPE_RAINY:
    {
        uint8_t temp8 = 3 - disp_ctrl.auto_refresh_count % 4;
        draw_on_chip_image(26, 53 + 30 - 5 * temp8, IMAGE_USER_WEATHER_RAINY);
        draw_on_chip_image(20, 53, IMAGE_USER_WEATHER_CLOUD_COM);
    }
    break;
    case WEATHER_TYPE_HAZE:
        draw_on_chip_image(20, 66, IMAGE_USER_WEATHER_HAZE);
        switch (disp_ctrl.auto_refresh_count % 3)
        {
        case 2:
            draw_on_chip_gray_image(20, 90, IMAGE_USER_WEATHER_HAZE_DOT, 0x7bcf);
            //不break,继续显示图标
        case 1:
            draw_on_chip_image(20, 59, IMAGE_USER_WEATHER_HAZE_DOT);
            break;
        default:
            break;
        }
        break;
    case WEATHER_TYPE_SNOW:
    {
        uint8_t temp8 = 3 - disp_ctrl.auto_refresh_count % 4;
        draw_on_chip_image(25, 53 + 30 - 5 * temp8, IMAGE_USER_WEATHER_SNOW);
        draw_on_chip_image(20, 53, IMAGE_USER_WEATHER_CLOUD_COM);
    }
    break;
    case WEATHER_TYPE_CLOUDY:
        draw_on_chip_gif_array(15, 59, zip_gif_WEATHER_CLOUDY, sizeof(zip_gif_WEATHER_CLOUDY) / sizeof(zip_gif_WEATHER_CLOUDY[0]));
        break;
    case WEATHER_TYPE_THUNDER:
        draw_on_chip_gif_array(21 + 4, 81, zip_gif_WEATHER_THUNDER, sizeof(zip_gif_WEATHER_THUNDER) / sizeof(zip_gif_WEATHER_THUNDER[0]));
        draw_on_chip_image(20, 53, IMAGE_USER_WEATHER_CLOUD_COM);
        break;
    default:
        break;
    }
}
view_event_result_t view_weather_event(int view, event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
        return view_event_result_handled;
    case event_view_entry:
    {
        weather_record_t weather = {0};
        weather_ctrl_get(0, &weather); //获取天气
        LOG("weather date %d", weather.date_index);
        if (weather.date_index == 0)
        {
            event_button_front_press_broadcast();
        }
    }
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
